<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>老师仪表盘 - 局域网视频学习系统</title>
    <link rel="stylesheet" href="../css/common.css">
    <style>
        .dashboard-header {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: var(--shadow);
            margin-bottom: 20px;
        }

        .welcome-text {
            font-size: 24px;
            font-weight: bold;
            color: var(--neutral-dark);
            margin-bottom: 8px;
        }

        .welcome-subtitle {
            color: #666;
            font-size: 14px;
        }

        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }

        .stat-card {
            background: white;
            border-radius: 8px;
            padding: 20px;
            box-shadow: var(--shadow);
            text-align: center;
            transition: transform 0.3s ease;
            border-top: 4px solid var(--teacher-color);
        }

        .stat-card:hover {
            transform: translateY(-2px);
        }

        .stat-icon {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background: var(--teacher-color);
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 12px;
            font-size: 20px;
        }

        .stat-number {
            font-size: 28px;
            font-weight: bold;
            color: var(--neutral-dark);
            margin-bottom: 6px;
        }

        .stat-label {
            color: #666;
            font-size: 14px;
        }

        .content-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
        }

        .recent-videos {
            background: white;
            border-radius: 8px;
            box-shadow: var(--shadow);
            overflow: hidden;
        }

        .recent-videos .card-header {
            background: var(--teacher-color);
            color: white;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .video-item {
            padding: 16px 20px;
            border-bottom: 1px solid var(--border-color);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .video-item:last-child {
            border-bottom: none;
        }

        .video-info h4 {
            font-size: 14px;
            font-weight: 600;
            margin-bottom: 4px;
            color: var(--neutral-dark);
        }

        .video-meta {
            font-size: 12px;
            color: #666;
        }

        .video-actions {
            display: flex;
            gap: 8px;
        }

        .student-stats {
            background: white;
            border-radius: 8px;
            box-shadow: var(--shadow);
            overflow: hidden;
        }

        .student-stats .card-header {
            background: var(--success-color);
            color: white;
        }

        .chart-container {
            padding: 20px;
            height: 200px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #666;
        }

        .quick-actions {
            background: white;
            border-radius: 8px;
            box-shadow: var(--shadow);
            padding: 20px;
            margin-bottom: 20px;
        }

        .actions-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
        }

        .action-card {
            padding: 20px;
            border: 2px solid var(--border-color);
            border-radius: 8px;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s ease;
            text-decoration: none;
            color: inherit;
        }

        .action-card:hover {
            border-color: var(--teacher-color);
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(78, 205, 196, 0.2);
        }

        .action-icon {
            font-size: 32px;
            margin-bottom: 12px;
            color: var(--teacher-color);
        }

        .action-title {
            font-size: 16px;
            font-weight: 600;
            margin-bottom: 6px;
            color: var(--neutral-dark);
        }

        .action-desc {
            font-size: 12px;
            color: #666;
        }

        .empty-state {
            text-align: center;
            padding: 40px 20px;
            color: #666;
        }

        .empty-state i {
            font-size: 48px;
            margin-bottom: 16px;
            color: #ccc;
        }

        .sidebar-footer {
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
            padding: 20px;
            border-top: 1px solid var(--border-color);
        }

        .logout-btn {
            width: 100%;
            padding: 12px 16px;
            background: var(--error-color);
            color: white;
            border: none;
            border-radius: 6px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
        }

        .logout-btn:hover {
            background: #c82333;
            transform: translateY(-1px);
        }

        /* 考试信息样式 - 横向布局，扁平化设计 */
        .exam-info {
            background: var(--card-bg);
            border: 1px solid var(--border-color);
            border-radius: 8px;
            padding: 20px;
            display: flex;
            align-items: center;
            gap: 24px;
            transition: all 0.3s ease;
        }

        .exam-info:hover {
            border-color: var(--primary-color);
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }

        .exam-header {
            flex: 1;
            display: flex;
            flex-direction: column;
            gap: 8px;
        }

        .exam-title {
            display: flex;
            align-items: center;
            gap: 12px;
            margin-bottom: 8px;
        }

        .exam-title h4 {
            margin: 0;
            font-size: 16px;
            font-weight: 600;
            color: var(--text-primary);
        }

        .exam-status {
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 11px;
            font-weight: 500;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .exam-status.published {
            background: #e8f5e8;
            color: #2e7d32;
        }

        .exam-status.draft {
            background: #fff3e0;
            color: #f57c00;
        }

        .exam-details {
            flex: 2;
            display: flex;
            gap: 32px;
        }

        .detail-item {
            display: flex;
            flex-direction: column;
            gap: 4px;
        }

        .detail-item .label {
            font-size: 12px;
            color: var(--text-secondary);
            font-weight: 500;
        }

        .detail-item .value {
            font-size: 14px;
            color: var(--text-primary);
            font-weight: 600;
        }

        .exam-actions {
            flex: 0 0 auto;
        }

        .exam-actions .btn {
            background: var(--primary-color);
            color: white;
            border: none;
            padding: 8px 16px;
            font-size: 13px;
            font-weight: 500;
            border-radius: 6px;
            transition: all 0.3s ease;
            cursor: pointer;
        }

        .exam-actions .btn:hover {
            background: var(--primary-hover);
            transform: translateY(-1px);
        }

        @media (max-width: 768px) {
            .content-grid {
                grid-template-columns: 1fr;
            }
            
            .stats-grid {
                grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
            }
            
            .actions-grid {
                grid-template-columns: 1fr;
            }

            /* 移动端考试信息卡片样式 */
            .exam-info {
                flex-direction: column;
                align-items: stretch;
                gap: 16px;
            }

            .exam-header {
                flex: none;
            }

            .exam-details {
                flex: none;
                flex-direction: column;
                gap: 12px;
            }

            .detail-item {
                flex-direction: row;
                justify-content: space-between;
                align-items: center;
                padding: 8px 0;
                border-bottom: 1px solid var(--border-light);
            }

            .detail-item:last-child {
                border-bottom: none;
            }

            .exam-actions {
                flex: none;
                text-align: center;
            }
        }
    </style>
</head>
<body>
    <!-- 侧边栏 -->
    <div class="sidebar">
        <div class="sidebar-header">
            <h3>老师工作台</h3>
        </div>
        <ul class="sidebar-nav">
            <li class="nav-item">
                <a href="dashboard.html" class="nav-link active">
                    <i>📊</i> 仪表盘
                </a>
            </li>
            <li class="nav-item">
                <a href="upload.html" class="nav-link">
                    <i>📤</i> 上传视频
                </a>
            </li>
            <li class="nav-item">
                <a href="videos.html" class="nav-link">
                    <i>🎥</i> 视频管理
                </a>
            </li>
            <li class="nav-item">
                <a href="exam-management.html" class="nav-link">
                    <i>📝</i> 考试管理
                </a>
            </li>
            <li class="nav-item">
                <a href="question-management.html" class="nav-link">
                    <i>❓</i> 题目管理
                </a>
            </li>
            <li class="nav-item">
                <a href="profile.html" class="nav-link">
                    <i>👤</i> 个人资料
                </a>
            </li>
        </ul>
        
        <!-- 退出登录 -->
        <div class="sidebar-footer">
            <button class="logout-btn" onclick="UserManager.logout()">
                <i>🚪</i> 退出登录
            </button>
        </div>
    </div>

    <!-- 主内容区 -->
    <div class="main-content">
        <div class="content-wrapper">
            <!-- 页面标题 -->
            <div class="dashboard-header">
                <h1 class="welcome-text">欢迎回来，<span data-user-name>老师</span>！</h1>
                <p class="welcome-subtitle">今天是 <span id="currentDate"></span>，以下是您的工作概览</p>
            </div>

            <!-- 快速操作 -->
            <div class="quick-actions">
                <h3 style="margin-bottom: 20px; color: var(--neutral-dark);">快速操作</h3>
                <div class="actions-grid">
                    <a href="upload.html" class="action-card">
                        <div class="action-icon">📤</div>
                        <div class="action-title">上传视频</div>
                        <div class="action-desc">上传新的教学视频</div>
                    </a>
                    <a href="videos.html" class="action-card">
                        <div class="action-icon">🎥</div>
                        <div class="action-title">视频管理</div>
                        <div class="action-desc">管理我的视频</div>
                    </a>
                    <a href="exam-management.html" class="action-card">
                        <div class="action-icon">📝</div>
                        <div class="action-title">创建考试</div>
                        <div class="action-desc">为学生创建测试</div>
                    </a>
                    <a href="question-management.html" class="action-card">
                        <div class="action-icon">❓</div>
                        <div class="action-title">添加题目</div>
                        <div class="action-desc">管理题库和题目</div>
                    </a>
                </div>
            </div>

            <!-- 统计卡片 -->
            <div class="stats-grid">
                <div class="stat-card">
                    <div class="stat-icon">📚</div>
                    <div class="stat-number" id="courseCount">-</div>
                    <div class="stat-label">我的课程</div>
                </div>
                <div class="stat-card">
                    <div class="stat-icon">🎥</div>
                    <div class="stat-number" id="videoCount">-</div>
                    <div class="stat-label">上传视频</div>
                </div>
                <div class="stat-card">
                    <div class="stat-icon">👁️</div>
                    <div class="stat-number" id="totalViews">-</div>
                    <div class="stat-label">总观看次数</div>
                </div>
                <div class="stat-card">
                    <div class="stat-icon">👨‍🎓</div>
                    <div class="stat-number" id="studentCount">-</div>
                    <div class="stat-label">学习学生</div>
                </div>
            </div>

            <!-- 内容区域 -->
            <div class="content-grid">
                <!-- 最近上传视频 -->
                <div class="recent-videos">
                    <div class="card-header">
                        <h3>最近上传视频</h3>
                        <a href="videos.html" style="color: white; text-decoration: none;">查看全部</a>
                    </div>
                    <div class="card-body" id="recentVideosList">
                        <div class="empty-state">
                            <i>🎥</i>
                            <p>暂无上传视频</p>
                            <a href="upload.html" class="btn btn-teacher btn-sm">立即上传</a>
                        </div>
                    </div>
                </div>

                <!-- 考试数据统计 -->
                <div class="student-stats">
                    <div class="card-header">
                        <h3>考试数据</h3>
                    </div>
                    <div class="chart-container" id="studentChart">
                        <div class="empty-state">
                            <i>📊</i>
                            <p>暂无考试数据</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 侧边栏切换按钮 -->
    <button class="sidebar-toggle">☰</button>

    <script src="../js/common.js"></script>
    <script>
        // 页面初始化
        document.addEventListener('DOMContentLoaded', function() {
            // 检查权限
            if (!UserManager.hasPermission(1)) {
                Message.error('权限不足');
                setTimeout(() => {
                    window.location.href = '../login.html';
                }, 2000);
                return;
            }

            // 设置当前日期
            const now = new Date();
            const dateStr = now.toLocaleDateString('zh-CN', {
                year: 'numeric',
                month: 'long',
                day: 'numeric',
                weekday: 'long'
            });
            document.getElementById('currentDate').textContent = dateStr;

            // 加载仪表盘数据
            loadDashboardData();
        });

        // 加载仪表盘数据
        async function loadDashboardData() {
            try {
                // 加载我的视频
                await loadMyVideos();
                
                // 加载统计数据
                await loadStats();
                
                // 加载考试数据
                await loadExamStats();
                
            } catch (error) {
                console.error('加载仪表盘数据失败:', error);
                Message.error('加载数据失败');
            }
        }

        // 加载我的视频
        async function loadMyVideos() {
            try {
                const response = await API.get('/video/my');
                if (response.success) {
                    const videos = response.videos;
                    document.getElementById('videoCount').textContent = videos.length;
                    
                    const container = document.getElementById('recentVideosList');
                    
                    if (videos.length === 0) {
                        container.innerHTML = `
                            <div class="empty-state">
                                <i>🎥</i>
                                <p>暂无上传视频</p>
                                <a href="upload.html" class="btn btn-teacher btn-sm">立即上传</a>
                            </div>
                        `;
                    } else {
                        const recentVideos = videos.slice(0, 5);
                        container.innerHTML = recentVideos.map(video => `
                            <div class="video-item">
                                <div class="video-info">
                                    <h4>${video.title}</h4>
                                    <div class="video-meta">${Utils.formatTime(video.uploadTime)}</div>
                                </div>
                                <div class="video-actions">
                                    <button class="btn btn-sm btn-primary" onclick="viewVideo(${video.id})">查看</button>
                                </div>
                            </div>
                        `).join('');
                    }
                }
            } catch (error) {
                console.error('加载我的视频失败:', error);
            }
        }

        // 加载统计数据
        async function loadStats() {
            try {
                // 这里可以根据实际需求加载更多统计数据
                // 暂时显示模拟数据
                document.getElementById('courseCount').textContent = '0';
                document.getElementById('totalViews').textContent = '0';
                document.getElementById('studentCount').textContent = '0';
            } catch (error) {
                console.error('加载统计数据失败:', error);
            }
        }

        // 加载考试数据
        async function loadExamStats() {
            try {
                const container = document.getElementById('studentChart');
                
                // 获取老师的考试列表
                const response = await API.get('/exam/teacher/list');
                if (response.success && response.exams && response.exams.length > 0) {
                    // 按创建时间排序，获取最新考试
                    const latestExam = response.exams.sort((a, b) => new Date(b.createTime) - new Date(a.createTime))[0];
                    
                    // 格式化时间显示
                    const formatDate = (dateString) => {
                        if (!dateString) return '未知';
                        try {
                            const date = new Date(dateString);
                            if (isNaN(date.getTime())) return '未知';
                            return date.toLocaleDateString('zh-CN', {
                                year: 'numeric',
                                month: '2-digit',
                                day: '2-digit',
                                hour: '2-digit',
                                minute: '2-digit'
                            });
                        } catch (e) {
                            return '未知';
                        }
                    };
                    
                    // 显示最新考试信息
                    container.innerHTML = `
                        <div class="exam-info">
                            <div class="exam-header">
                                <div class="exam-title">
                                    <h4>📝 ${latestExam.title || '未命名考试'}</h4>
                                    <span class="exam-status ${latestExam.status === 1 ? 'published' : 'draft'}">
                                        ${latestExam.status === 1 ? '已发布' : '草稿'}
                                    </span>
                                </div>
                            </div>
                            <div class="exam-details">
                                <div class="detail-item">
                                    <span class="label">题目数量</span>
                                    <span class="value">${latestExam.questionCount || 0} 题</span>
                                </div>
                                <div class="detail-item">
                                    <span class="label">考试时长</span>
                                    <span class="value">${latestExam.duration || 0} 分钟</span>
                                </div>
                                <div class="detail-item">
                                    <span class="label">创建时间</span>
                                    <span class="value">${formatDate(latestExam.createTime)}</span>
                                </div>
                            </div>
                            <div class="exam-actions">
                                <button class="btn" onclick="window.location.href='exam-management.html'">
                                    管理考试
                                </button>
                            </div>
                        </div>
                    `;
                } else {
                    // 没有考试时显示空状态
                    container.innerHTML = `
                        <div class="empty-state">
                            <i>📊</i>
                            <p>暂无考试数据</p>
                            <small>创建第一个考试开始使用</small>
                        </div>
                    `;
                }
            } catch (error) {
                console.error('加载考试数据失败:', error);
                const container = document.getElementById('studentChart');
                container.innerHTML = `
                    <div class="empty-state">
                        <i>❌</i>
                        <p>加载失败</p>
                        <small>请刷新页面重试</small>
                    </div>
                `;
            }
        }

        // 查看视频
        function viewVideo(videoId) {
            // 跳转到视频详情页面或打开模态框
            window.open(`../student/player.html?id=${videoId}`, '_blank');
        }

        // 定期刷新数据
        setInterval(() => {
            loadDashboardData();
        }, 60000); // 每分钟刷新一次
    </script>
</body>
</html>
